var express = require('express');
var router = express.Router();

var OAuth = require('wechat-oauth');
var client = new OAuth('wx361547ce36eb2185', '17fa4725b493ac5477c9ba174fd2456f');

/* GET home page. */
router.get('/', function (req, res, next) {
  var redirectUrl = "";

  var url = client.getAuthorizeURL(redirectUrl, 'state', 'scope');
  //重定向获取code
  res.redirectUrl(url);

});

router.get('/getCode', function (req, res, next) {
  var code = req.query.code;
  client.getAccessToken(code, function (err, result) {
    var accessToken = result.data.access_token;
    var openid = result.data.openid;
    res.json({
      accessToken: accessToken,
      openid: openid
    });
  });

});


module.exports = router;
